package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.redis.util.RedisKeys;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class LoginController {

    @Reference
    private IUserInfoService userInfoService;
    /**
     * 测试
     */
    @ResponseBody
    @RequestMapping("/hello")
    public List<UserInfo> hello(){
        return userInfoService.get();
    }

    @ResponseBody
    @RequestMapping("/checkPhone")
    public Boolean checkPhone(String phone){
        return !userInfoService.checkPhone(phone);
    }
    @ResponseBody
    @RequestMapping("/sendVerifyCode")
    public AjaxResult userRegist(String phone){
        userInfoService.sendVerifyCode(phone);
        return  AjaxResult.SUCCESS;
    }

    @ResponseBody
    @RequestMapping("/userRegist")
    public AjaxResult userRegist(String phone,String nickname,String password,String rpassword,String verifyCode){
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
        return  AjaxResult.SUCCESS;
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin (HttpServletResponse response,String username, String password){
        String token = userInfoService.userLogin(username,password);

        //将token放到cookie里面
        Cookie cookie = new Cookie("token",token);
        //设置浏览器返回路径,只要是路径为/,下次请求都会带上token值到服务器
        cookie.setPath("/");
        //设置cookie的过期时间
        cookie.setMaxAge(RedisKeys.USER_LOGIN.getTime().intValue());
        //将cookie返回给浏览器
        response.addCookie(cookie);
        //成功返回
        return  AjaxResult.SUCCESS;
    }

}
